package com.m3958.firstgwt.client.service;


import java.util.LinkedHashMap;

import com.google.gwt.user.client.Timer;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.m3958.firstgwt.client.constants.CommonFieldConstants;
import com.m3958.firstgwt.client.datasource.FieldEnumDataSource;
import com.m3958.firstgwt.client.types.FieldEnumField;
import com.m3958.firstgwt.client.types.ProtectLevel;
import com.m3958.firstgwt.shared.type.CommonField;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TextMatchStyle;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;


@Singleton
public class AppUiService {

	
	@Inject
	private FieldEnumDataSource fieldDs;
	
	@Inject
	private CommonFieldConstants cConstants;
	
	
	public SelectItem getProtectLevelItem(){
	    SelectItem protectLevelItem = new SelectItem(CommonField.PROTECT_LEVEL.getValue(),cConstants.protectLevel());
	    LinkedHashMap<String, String> vm = new LinkedHashMap<String, String>();
	    vm.put(ProtectLevel.PUBLIC,"公共" );
	    vm.put(ProtectLevel.LOGIN,"登录用户");
	    vm.put(ProtectLevel.PERMISSION,"有权限" );
	    vm.put(ProtectLevel.CREATOR,"主人");
	    vm.put(ProtectLevel.PUBLIC_ADD,"公共写入" );
	    protectLevelItem.setValueMap(vm);
	    
	    protectLevelItem.setDefaultValue("");
	    return protectLevelItem;
	}
	

	public ComboBoxItem createCombItem(String ename,String cname,String pname){
		ComboBoxItem item = new ComboBoxItem(ename, cname);
	    Criteria minzuC = new Criteria();
	    minzuC.addCriteria(FieldEnumField.FIELD_TYPE.getValue(), pname);
	    item.setOptionDataSource(fieldDs);
	    item.setValueField("fieldValue");
	    item.setAnimatePickList(true);
	    item.setTextMatchStyle(TextMatchStyle.EXACT);
	    item.setPickListCriteria(minzuC);
		return item;
	}
	
	public SelectItem createSelectItem(String ename,String cname,String pname){
		SelectItem item = new SelectItem(ename, cname);
	    Criteria minzuC = new Criteria();
	    minzuC.addCriteria(FieldEnumField.FIELD_TYPE.getValue(), pname);
	    item.setOptionDataSource(fieldDs);
	    item.setValueField("fieldValue");
	    item.setAnimatePickList(true);
	    item.setTextMatchStyle(TextMatchStyle.EXACT);
	    item.setPickListCriteria(minzuC);
		return item;
	}
	
	private Label promptLabel;
	
	private Label getLb(){
		if(promptLabel == null){
			promptLabel = new Label();
		}
		return promptLabel;
	}
	
	private Window promptWindow;
	
	private Window getPw(){
		if(promptWindow == null){
			promptWindow = new Window();
			promptWindow.setHeight(120);
			promptWindow.setWidth(450);
			promptWindow.centerInPage();
			promptWindow.setShowTitle(false);
			promptWindow.setShowFooter(false);
			promptWindow.setShowCloseButton(false);
			promptWindow.setShowMaximizeButton(false);
			promptWindow.setShowMinimizeButton(false);
			promptLabel.setHeight(40);
			VLayout l = new VLayout();
			l.setWidth100();
			l.setHeight100();
			l.setAlign(Alignment.CENTER);
			l.setAlign(VerticalAlignment.CENTER);
			l.addMember(getLb());
			promptWindow.addItem(l);
		}
		return promptWindow;
	}
	
	
	
	public TextItem getPerpageItem(boolean isVertital){
		TextItem perpageItem = new TextItem(CommonField.PER_PAGE.getValue(), cConstants.perpage());
		if(isVertital)
			perpageItem.setTitleOrientation(TitleOrientation.TOP);
		return perpageItem;
	}
	
	public HiddenItem getIdHiddenItem(){
		HiddenItem idItem = new HiddenItem(CommonField.ID.getValue());
		return idItem;
	}
	
	public TextItem getIdDisableItem(boolean isVertical){
		TextItem idItem = new TextItem(CommonField.ID.getValue(),CommonField.ID.getValue());
		if(isVertical)
			idItem.setTitleOrientation(TitleOrientation.TOP);
		idItem.setDisabled(true);
		return idItem;
	}
	
	public HiddenItem getSiteIdItem(){
		HiddenItem idItem = new HiddenItem(CommonField.SITE_ID.getValue());
		return idItem;
	}
	
	public TextItem getPositionItem(){
		TextItem positionItem = new TextItem(CommonField.POSITION.getValue(),cConstants.position());
		return positionItem;
	}
	
	public HiddenItem getVersionHiddenItem(){
		HiddenItem versionItem = new HiddenItem(CommonField.VERSION.getValue());
		return versionItem;
	}
	
	public HiddenItem getCreatedAtHiddenItem(){
		HiddenItem createdAtItem = new HiddenItem(CommonField.CREATED_AT.getValue());
		return createdAtItem;
	}
	
	public final void showTmpPrompt(String prompt,int millis){
		String s = "<div  align=\"center\">" + prompt + "</div>";
		getLb().setContents(s);
		getPw().show();
		Timer t = new Timer(){
			@Override
			public void run() {
				getPw().hide();
			}
		};
		t.schedule(millis);
	}
	
	public final void showTmpPrompt(String prompt){
		String s = "<div  align=\"center\">" + prompt + "</div>";
		getLb().setContents(s);
		getPw().show();
		Timer t = new Timer(){
			@Override
			public void run() {
				getPw().hide();
			}
		};
		t.schedule(1000);
	}
	
	public int[] getLeftAndTopIfCenter(int boxwidth,int boxheight){
		int hl = Page.getWidth();
		int vl = Page.getHeight();
		int left = (hl - boxwidth)/2;
		int top = (vl - boxheight)/2;
		return new int[]{left,top};
	}
	
	public int getLeftGapIfCenter(int boxwidth){
		int hl = Page.getWidth();
		return (hl - boxwidth)/2;
	}
	
	public int getTopGapIfCenter(int boxheight){
		int vl = Page.getHeight();
		return (vl - boxheight)/2;
	}

}
